package com.coustemview.qianfeng.huaqiangbeistore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.coustemview.qianfeng.huaqiangbeistore.R;
import com.coustemview.qianfeng.huaqiangbeistore.activity.Mainpage_oversear_detailsActivity;
import com.coustemview.qianfeng.huaqiangbeistore.adapter.voersear_listview_adapter;
import com.coustemview.qianfeng.huaqiangbeistore.config.Netconfig;
import com.coustemview.qianfeng.huaqiangbeistore.model.Mainpage_Oversear_Bean;
import com.coustemview.qianfeng.huaqiangbeistore.model.Mainpage_Oversear_handler;
import com.coustemview.qianfeng.huaqiangbeistore.model.Mainpage_Oversear_position;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mingma on 2016/8/29.
 */
public class Fragment_mainpage_oversear extends Fragment {
    private LinearLayout oversear_ll_floating;
   private ListView mListView;
    private ImageView image_listviewhead2;
    private List<Mainpage_Oversear_handler>data;
    private BaseAdapter mAdapter;
    private View view1;
    private Button mButton;
    private View view2;
    private RadioGroup mRadioGroup;
    private int position1,position2,position3;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mainpage_oversear, null);
        view2 = inflater.inflate(R.layout.oversear_listview_floating, null);
        mRadioGroup= (RadioGroup) view2.findViewById(R.id.radiogroup_voersear_floating);

        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
      mButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              Intent intent=new Intent(getActivity(), Mainpage_oversear_detailsActivity.class);
              getActivity().startActivity(intent);
          }
      });

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                  switch (checkedId) {
                      case R.id.radiobutton_oversear_floating1:
                         mListView.setSelectionFromTop(2,0);
                          break;
                      case R.id.radiobutton_oversear_floating2:
                         mListView.setSelectionFromTop(position1+2,0);
                          break;
                      case R.id.radiobutton_oversear_floating3:
                          mListView.setSelectionFromTop(position1+position2+2,0);
                          break;
                      default:
                          break;
                  }
            }
        });

        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    oversear_ll_floating.setVisibility(View.VISIBLE);
                } else {
                    oversear_ll_floating.setVisibility(View.GONE);
                }
            }
        });
    }

    private void setData() {
        mListView.addHeaderView(view1);
        mListView.addHeaderView(view2);
        mListView.setAdapter(mAdapter);
    }

    private void initData() {
        data=new ArrayList<>();
        mAdapter=new voersear_listview_adapter(data,getActivity());
        //头布局
        view1 = LayoutInflater.from(getActivity()).inflate(R.layout.mainpage_oversear_listviewhead, null);
        image_listviewhead2= (ImageView) view1.findViewById(R.id.image_oversear_listviewhead2);
        mButton= (Button) view1.findViewById(R.id.button_oversear_details);


        RequestParams params=new RequestParams(Netconfig.MAINPAGE_OVERSEAR_PATH);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
             if(result!=null) {
                 try {
                     JSONObject object=new JSONObject(result);
                     JSONObject data = object.getJSONObject("data");
                     String baoBanner = data.getString("baoBanner");
                     Picasso.with(getActivity()).load(baoBanner).into(image_listviewhead2);
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }
             }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
          Toast.makeText(getActivity(), "网络错误,请检查网络", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

      x.http().get(params, new Callback.CommonCallback<String>() {
          @Override
          public void onSuccess(String result) {
               if(result!=null) {
               List<Mainpage_Oversear_handler> list = Mainpage_Oversear_Bean.getinfo(result);
                   Mainpage_Oversear_position getposition =  Mainpage_Oversear_Bean.getposition(result);
                   position1=getposition.position1;
                   position2=getposition.position2;
                   position3=getposition.position3;
                   data.addAll(list);
                   mAdapter.notifyDataSetChanged();
               }
          }

          @Override
          public void onError(Throwable ex, boolean isOnCallback) {
              Toast.makeText(getActivity(), "网络错误,请检查网络", Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onCancelled(CancelledException cex) {

          }

          @Override
          public void onFinished() {

          }
      });
    }

    private void initView(View view) {
        mListView= (ListView) view.findViewById(R.id.mainpage_oversear_listview);
        oversear_ll_floating= (LinearLayout) view.findViewById(R.id.oversear_ll_floating);
    }
}
